package com.jues.huichain.login.presenter;


import com.jues.huichain.login.activity.LoginActivity;
import com.jues.huichain.login.entity.CodeEntity;
import com.jues.huichain.login.entity.EntranceEntity;
import com.jues.huichain.login.entity.EntranceResultEntity;
import com.jues.huichain.login.model.EntranceModel;

import health.lm.com.component_base.base.mvp.contract.XPresent;
import health.lm.com.data.App;
import health.lm.com.data.HttpCST;
import health.lm.com.network.callback.SimpleCallBack;

/**
 * 登录
 */

public class LoginPresenter extends XPresent<LoginActivity> {
    public void login(String username, String code, boolean type) {
        if (!type) {
            //手机号验证码登录
            EntranceEntity entranceEntity = new EntranceEntity(username, HttpCST.TYPE_LOGIN, code);
            entranceEntity.setAccess_token("");
            EntranceModel.getInstance().login(entranceEntity, new SimpleCallBack<EntranceResultEntity.ResultDataBean>() {
                @Override
                public void onSuccess(EntranceResultEntity.ResultDataBean pResultDataBean) {
                    App.getModel().setAccess_token(pResultDataBean.getAccess_token());
                    App.getModel().setUid(pResultDataBean.getUser_id());
                    if (hasV()) getV().loginSuccess("登陆成功");
                }
            });
        } else {
            //密码登录
            EntranceEntity entranceEntity = new EntranceEntity();
            entranceEntity.setMobile(username);
            entranceEntity.setPassword(code);
            entranceEntity.setAccess_token("");
            EntranceModel.getInstance().login(entranceEntity, new SimpleCallBack<EntranceResultEntity.ResultDataBean>() {
                @Override
                public void onSuccess(EntranceResultEntity.ResultDataBean pResultDataBean) {
                    App.getModel().setAccess_token(pResultDataBean.getAccess_token());
                    App.getModel().setUid(pResultDataBean.getUser_id());
                    if (hasV()) getV().loginSuccess("登陆成功");
                }
            });
        }
    }

    public void getCode(String phone) {
        CodeEntity codeEntity = new CodeEntity("", phone, HttpCST.TYPE_LOGIN);
        EntranceModel.getInstance().getCode(codeEntity, new SimpleCallBack<Object>() {
            @Override
            public void onSuccess(Object pO) {
                if (hasV()) getV().getCodeSuccess("验证码发送成功");
            }
        });
    }

    public void setPushCode(String push_code) {
        EntranceModel.getInstance().appPush(push_code, new SimpleCallBack<Object>() {
            @Override
            public void onSuccess(Object pO) {

            }
        });
    }
}
